$(function () {
    $.ajax({
        type: "GET",
        url: "/user/check-login",
        success: function (result) {
            if (result.code === "0") {
                location.href = "/page/watch";
            } else if (result.code === "101001") {
                doLogin();
            } else {
                $.toast.prototype.defaults.duration = 1000;//1秒
                $.toast(result.message, 'text');
            }
        },
        error: function () {
            $.toast.prototype.defaults.duration = 1000;//1秒
            $.toast("无网络", 'text');
        }
    });

});

function doLogin() {
    var url = window.location.href.split("?code=");
    var argsIndex = url[1].split("&state=");
    var code = argsIndex[0];
    var jsonData = {"code": code};
    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json",
        url: "/user/wechat-login",
        data: JSON.stringify(jsonData),
        success: function (result) {
            if (result.code === "0") {
                location.href = "/page/watch";
            } else {
                $.toast.prototype.defaults.duration = 1000;//1秒
                $.toast(result.message, 'text');
            }
        },
        error: function () {
            $.toast.prototype.defaults.duration = 1000;//1秒
            $.toast("无网络", 'text');
        }
    });
}